﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;

public partial class controls_admin_GroupAdminEdit : Pages_Base
{
    private string arrModuleID;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadParentGroups();
            loadData();
        }
    }
    protected void loadData()
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = BO_Sec_Group_Admin.Search(Convert.ToInt32(Request["g_id"]));
            if (objDT != null && objDT.Rows.Count > 0)
            {
                txtGroupName.Text = objDT.Rows[0]["GROUP_NAME"].ToString();
                txtDesc.Text = objDT.Rows[0]["DESCRIPTION"].ToString();
                ddlParentGroup.SelectedValue = objDT.Rows[0]["PARENT_GROUP_ID"].ToString();
                getSec_Admin_Modules(objDT.Rows[0]["ADMIN_MODULE_ID"].ToString());
            }
        }
        catch { }
        finally { base.CloseConnection(); }
    }
    private string[] getPerPermisstion(string permisstion)
    {
        string[] perPermisstion = permisstion.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        return perPermisstion;
    }
    protected void getSec_Admin_Modules(string permisstion)
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = BO_Sec_Admin_Modules.SelectPermission(0);
            if (objDT.Rows.Count > 0)
            {
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    string amID = objDT.Rows[i]["ADMIN_MODULE_ID"].ToString();
                    ListItem item = new ListItem(objDT.Rows[i]["DESCRIPTION"].ToString(), amID);
                    foreach (string per in getPerPermisstion(permisstion))
                    {
                        if (amID.Equals(per))
                        {
                            item.Selected = true;
                            break;
                        }
                    }
                    lstPermission.Items.Add(item);
                    getChildSec_Admin_Modules(Convert.ToInt32(amID), "", permisstion);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void getChildSec_Admin_Modules(int _parent_id, string ident, string permisstion)
    {
        try
        {
            DataTable objDT = BO_Sec_Admin_Modules.SelectPermission(_parent_id);
            if (objDT.Rows.Count > 0)
            {
                ident += "----- ";
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    string amID = objDT.Rows[i]["ADMIN_MODULE_ID"].ToString();
                    ListItem item = new ListItem(ident + objDT.Rows[i]["DESCRIPTION"].ToString(), amID);
                    foreach (string per in getPerPermisstion(permisstion))
                    {
                        if (amID.Equals(per))
                            item.Selected = true;
                    }
                    lstPermission.Items.Add(item);
                    getChildSec_Admin_Modules(Convert.ToInt32(amID), ident, permisstion);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void loadParentGroups()
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = BO_Sec_Group_Admin.SelectParent(1, 0);

            if (objDT.Rows.Count > 0)
            {
                ddlParentGroup.Items.Clear();
                ddlParentGroup.Items.Add(new ListItem("Nhóm cao nhất", "0"));
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    ddlParentGroup.Items.Add(new ListItem("---" + objDT.Rows[i]["GROUP_NAME"].ToString(), objDT.Rows[i]["GROUP_ADMIN_ID"].ToString()));
                    loadChildGroups(Convert.ToInt32(objDT.Rows[i]["GROUP_ADMIN_ID"].ToString()), "---");
                }
            }
            else
            {
                ddlParentGroup.Items.Add(new ListItem("Chưa có dữ liệu", "-1"));
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void loadChildGroups(int _parent_id, string ident)
    {
        try
        {

            DataTable objDT = BO_Sec_Group_Admin.SelectParent(1, _parent_id);

            if (objDT.Rows.Count > 0)
            {
                ident += "--- ";
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    ddlParentGroup.Items.Add(new ListItem(ident + objDT.Rows[i]["GROUP_NAME"].ToString(), objDT.Rows[i]["GROUP_ADMIN_ID"].ToString()));
                    loadChildGroups(Convert.ToInt32(objDT.Rows[i]["GROUP_ADMIN_ID"].ToString()), ident);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void getpermisstion(int parent_id)
    {
        DataTable objDT = BO_Sec_Admin_Modules.SelectPermission(parent_id);
        if (objDT.Rows.Count > 0)
            foreach (DataRow dr in objDT.Rows)
            {
                getpermisstion(Convert.ToInt32(dr["ADMIN_MODULE_ID"]));
            }
        else
            arrModuleID += parent_id.ToString() + ";";
    }
    protected void btnUpdateGroup_Click(object sender, EventArgs e)
    {
        string group_name = txtGroupName.Text;
        string group_desc = txtDesc.Text;
        int parent_group_id;
        try
        {
            base.OpenConnection();
            DataTable dtData = BO_Sec_Group_Admin.Search(Convert.ToInt32(Request["g_id"]));
            DataTable admin = (DataTable)Session["Admin"];
            parent_group_id = Convert.ToInt32(ddlParentGroup.SelectedValue);
            foreach (int index in lstPermission.GetSelectedIndices())
            {
                getpermisstion(Convert.ToInt32(lstPermission.Items[index].Value));
            }
            //DataTable objDT = (DataTable)Session["admin"];
            string strResult = BO_Sec_Group_Admin.Update(Convert.ToInt32(Request["group_id"]), parent_group_id, arrModuleID, txtGroupName.Text, txtDesc.Text, 1, Convert.ToDateTime(dtData.Rows[0]["created_date"].ToString()), dtData.Rows[0]["created_by"].ToString(), DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), Boolean.Parse(dtData.Rows[0]["STATUS"].ToString()));
            if (strResult != "0")
            {
                divMessage.InnerHtml = O2S_Message.Success("Hệ Thống", "Thông tin nhóm đã được cập nhật!");
            }
            else
            {
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Có lỗi trong quá trình xử lý, thử lại sau!");
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
}
